package cn.yh.register.hosp.service.impl;

import cn.yh.register.hosp.repository.DepartmentRepository;
import cn.yh.register.hosp.service.DepartmentService;
import cn.yh.register.model.hosp.Department;
import cn.yh.register.vo.hosp.DepartmentQueryVo;
import cn.yh.register.vo.hosp.DepartmentVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    DepartmentRepository departmentRepository;

    /**
     * 保存科室信息
     * @param objectMap  科室信息
     */
    @Override
    public void saveDepartment(Map<String, Object> objectMap) {
        //转对象
        Department department = JSONObject.parseObject(JSONObject.toJSONString(objectMap), Department.class);
        //先查询数据库中是否有
        Department dept = departmentRepository.findByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        if (dept == null) {
            //没有新增
            department.setCreateTime(new Date());
            department.setIsDeleted(0);
        }else {
            //有  修改
            department.setId(dept.getId());
            department.setCreateTime(dept.getCreateTime());
            department.setIsDeleted(dept.getIsDeleted());
        }
        department.setUpdateTime(new Date());
        departmentRepository.save(department);

    }

    /**
     *  分页查询医院的排班信息
     * @param current 当前页
     * @param limit  每页条数
     * @param queryVo 查询条件
     * @return
     */

    @Override
    public Page<Department> findDeptList(Integer current, Integer limit, DepartmentQueryVo queryVo) {
        Department department = new Department();
        department.setIsDeleted(0);
        BeanUtils.copyProperties(queryVo,department);

        //条件对象
        Example<Department> example = Example.of(department, ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)  //按照创建时间
                .withIgnoreCase(true));    // 忽略大小写
        //分页对象
        PageRequest pageRequest = PageRequest
                .of(current - 1, limit, Sort.by(Sort.Direction.DESC, "createTime"));//按创建时间降序
        return departmentRepository.findAll(example,pageRequest);
    }

    /**
     * 删除医院科室
     *
     * @param hoscode 医院code
     * @param depcode 科室code
     */
    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        departmentRepository.delete(department);
    }
    /**
     * 查询树型结构的科室排班列表
     *
     * @param hoscode 科室编号
     * @return 医院编号
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //组装条件
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        // 查询该医院的所有科室
        List<Department> departmentList = departmentRepository.findAll(example);
        // 为了方便操作将list转为map,key:大科室编号，value:该大科室对应的小科室列表
        Map<String, List<Department>> map = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map 组装大科室列表
        List<DepartmentVo> bigVoList = new ArrayList<>();
        for (Map.Entry<String, List<Department>> entry : map.entrySet()) {
            // key:大科室列表
            String key = entry.getKey();
            // value:该大科室的小科室列表
            List<Department> smallList = entry.getValue();
            //组装大科室
            DepartmentVo bigVo = new DepartmentVo();
            bigVo.setDepcode(key);
            bigVo.setDepname(smallList.get(0).getBigname());
            //组装小科室列表
            List<DepartmentVo> smallVoList = new ArrayList<>();
            for (Department dept : smallList) {
                DepartmentVo samllVo = new DepartmentVo();
                samllVo.setDepcode(dept.getDepcode());
                samllVo.setDepname(dept.getDepname());
                smallVoList.add(samllVo);
            }
            //将小科室列表放入大科室
            bigVo.setChildren(smallVoList);
            bigVoList.add(bigVo);
        }
        return bigVoList;
    }

    /**
     * 查询科室信息
     *
     * @param hoscode 医院编号
     * @param depcode 科室编号
     * @return
     */
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
    }

}
